/**
 * read file text
 * @param el input Element
 * @param encoding file encoding, eg. utf8, gbk
 * @example const res = await read(el);
 */
export default function read(el: HTMLInputElement, encoding = 'utf8') {
	const file = el && el.files && el.files[0];	// ! just read first file.
	if (!file) {
		throw new Error('There is no file could be read!');
	} else {
		return new Promise((resolve, reject) => {
			const r = new FileReader();
			r.readAsText(file, encoding);
			r.onload = (e) => {
				const file_string = (e.target as any).result as string;
				resolve(file_string);
			};
			r.onerror = (e) => {
				reject(e);
			};
		});
	}
}
